import { getUserProfile, getUserProfileById } from '@/api/user'
import { setToken, getToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    getToken(state, token) {
      state.token = token
      setToken(token)
    },
    // 清空token
    delToken(state) {
      state.token = ''
    },
    // 接收用户信息
    updataUserInfo(start, info) {
      start.userInfo = info
    },
    // 清空用户信息
    delUserInfo(start) {
      start.userInfo = {}
    }
  },
  actions: {
    // 发送axios 将token储存到vuex中
    async getUserInfo(context) {
      try {
        const res = await getUserProfile()
        console.log('获取用户信息', res)

        // 获取用户详细资料头像
        const info = await getUserProfileById(res.data.userId)
        console.log('info', info.data)
        context.commit('updataUserInfo', { ...res.data, ...info.data })
      } catch (error) {
        console.log(error)
      }
    }

  }
}

